package com.halloon.android.ui.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.halloon.android.R;
import com.halloon.android.adapter.TweetDetailAdapter;
import com.halloon.android.bean.TweetBean;
import com.halloon.android.data.ContentManager;
import com.halloon.android.view.PinnedHeaderListView;

public class TweetDetailFragment extends SherlockFragment implements OnClickListener{
	private TweetDetailFragmentCallback tdCallback;
	private Context context;
	private PinnedHeaderListView pinnedHeaderListView;
	private Button backButton;
	private TweetDetailAdapter tweetDetailAdapter;
	private TweetBean tweetDetailBean = new TweetBean();
	private ArrayList<TweetBean> tweetCommentBean = new ArrayList<TweetBean>();
	private String id;
	private View loadingView;
	
	public interface TweetDetailFragmentCallback{
		public void setupProfileFragment(Bundle bundle);
		public void destroyDetailFragment();
		public void mListChange(int status);
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
		this.tdCallback = (TweetDetailFragmentCallback) activity;
		if(getArguments().getString("id") != null) this.id = getArguments().getString("id");
		context = activity;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View root = inflater.inflate(R.layout.tweet_detail, null, false);
		
		loadingView = (View) root.findViewById(R.id.loading);
		pinnedHeaderListView = (PinnedHeaderListView) root.findViewById(R.id.pinned_header);
		
		backButton = (Button) root.findViewById(R.id.back_button);
		backButton.setOnClickListener(this);
		
		loadData();
		
		return root;
	}
	
	private void loadData(){
		AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>(){
			@Override
			protected void onPreExecute(){
				loadingView.setVisibility(View.VISIBLE);
				pinnedHeaderListView.setVisibility(View.GONE);
			}
			
			@Override
			protected String doInBackground(Void... params){
				tweetDetailBean = ContentManager.getInstance(context).getTweetFromId(id);
				ArrayList<TweetBean> tmp_list = ContentManager.getInstance(context).getTweetCommentFromId(id, "1");
				if(tweetDetailBean == null || tweetDetailBean.getText() == null){
					return "failure";
				}
				
				tweetCommentBean.clear();
				tweetCommentBean.addAll(tmp_list);
				
				return "success";
			}
			
			@Override
			protected void onPostExecute(String result){
				if(result.equals("success")){
					loadingView.setVisibility(View.GONE);
					pinnedHeaderListView.setVisibility(View.VISIBLE);
					pinnedHeaderListView.setPinnedHeaderView(getSherlockActivity().getLayoutInflater().inflate(R.layout.tweet_detail_mcount, pinnedHeaderListView, false));
					
					tweetDetailAdapter = new TweetDetailAdapter(context, tweetDetailBean, tweetCommentBean);
					
					pinnedHeaderListView.setAdapter(tweetDetailAdapter);
					pinnedHeaderListView.setOnScrollListener(tweetDetailAdapter);
					tweetDetailAdapter.notifyDataSetChanged();
				}else{
					tdCallback.destroyDetailFragment();
					Toast.makeText(context, context.getString(R.string.tweet_not_exist), Toast.LENGTH_LONG).show();
				}
			}
		};
		
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
			task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}else{
			task.execute();
		}
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		pinnedHeaderListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				if(position == 0){
					Bundle bundle = new Bundle();
					bundle.putString("name", tweetDetailBean.getName());
					bundle.putInt("type", TabProfileFragment.OTHER);
					tdCallback.setupProfileFragment(bundle);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.back_button:
			tdCallback.destroyDetailFragment();
			break;
		case R.id.title_text:
			break;
		}
	}
	
	public void changeMList(int status){
		ArrayList<TweetBean> tmp_tweetBeanList = new ArrayList<TweetBean>();
		
		if(status == TweetDetailAdapter.COMMENT_STATUS_COMMENT){
			tmp_tweetBeanList = ContentManager.getInstance(context).getTweetCommentFromId(id, "1");
		}else{
			tmp_tweetBeanList = ContentManager.getInstance(context).getTweetCommentFromId(id, "0");
		}
		
		tweetCommentBean.clear();
		tweetCommentBean.addAll(tmp_tweetBeanList);
		tweetDetailAdapter.notifyDataSetChanged();
	}
}
